﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.TonGiaoService;
namespace eTMS.Controllers{
public class TonGiaoControllerBase : CommonController{
	public TonGiaoControllerBase()
		: base()
	{	
		this.dao = new TonGiaoServiceClient();
	}
	~TonGiaoControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private TonGiaoEntity oTonGiao;	
	public TonGiaoEntity TonGiaoObject{
		get{
			return oTonGiao;
		}
		set{
			oTonGiao=value;
		}
	}
	protected TonGiaoServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSTonGiao(false);			
			return _Table;
		}
	}
	public void LoadAllDSTonGiao(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oTonGiao==null)return false;
		bool result=true;
		try{
			oTonGiao = dao.SaveRet(oTonGiao);
		}
		catch{
			result=false;
		}
		if(result){
			_ListTonGiao.Add(oTonGiao);
		}
		return result;
	}
	public bool Update(){
		if(oTonGiao==null)return false;
		return dao.Update(oTonGiao);
	}
	public bool Delete(){
		if(oTonGiao==null)return false;
		bool retvalue = dao.Delete(oTonGiao);
		if(retvalue){			
			_ListTonGiao.Remove(oTonGiao);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 TonGiao_ID){
		bool retvalue = dao.DeleteByID(TonGiao_ID);		
		if(retvalue){
			TonGiaoEntity oDeleted=FindTonGiao(TonGiao_ID);
			if(oDeleted!=null){
				_ListTonGiao.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oTonGiao = new TonGiaoEntity();
	}
	//Check if exists MaTonGiao
	public bool CheckExistsMaTonGiao(object value){	
		return dao.SelectOneByMaTonGiao(value.ToString()).MaTonGiao!=null;
    }    
    //Copy data from data row in grid to object
    public TonGiaoEntity MapDataRowToObject(DataRow row){
		TonGiaoEntity o = new TonGiaoEntity();
		 o.MaTonGiao=Convert.ToString(row["MaTonGiao"]);
		 o.TenTonGiao=Convert.ToString(row["TenTonGiao"]);
		 o.MoTaTonGiao=Convert.ToString(row["MoTaTonGiao"]);
		 if(!row.IsNull("TonGiao_ID"))
		    o.TonGiao_ID=(Int32)(row["TonGiao_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(TonGiaoEntity o,DataRow row){
		row["MaTonGiao"]=o.MaTonGiao;
		row["TenTonGiao"]=o.TenTonGiao;
		row["MoTaTonGiao"]=o.MoTaTonGiao;
		row["TonGiao_ID"]=o.TonGiao_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.TonGiaoObject,row);
    }
	public void LoadObject(Int32 TonGiao_ID){
		TonGiaoEntity oLoad=FindTonGiao(TonGiao_ID);
		if(oLoad==null){
			oTonGiao=this.LoadTonGiao(TonGiao_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oTonGiao.IsNullTonGiao_ID;
			if(!bNotIsNull){
				this.ListTonGiao.Add(oTonGiao);
			}
		}
		else{
			oTonGiao=oLoad;
		}
	}
	#region CachedList TonGiao
	private static List<TonGiaoEntity> _ListTonGiao=new List<TonGiaoEntity>();
    public List<TonGiaoEntity>ListTonGiao{
		get{
			return _ListTonGiao;
		}
	}
	public static List<TonGiaoEntity>ListTonGiaoStatic{
		get{
			return _ListTonGiao;
		}
	}
	public TonGiaoEntity FindTonGiao(Int32 TonGiao_ID){
		TonGiaoEntity oResult=this.ListTonGiao.Find(delegate(TonGiaoEntity oFind){
			bool result=true; 
			result&=oFind.TonGiao_ID.Equals(TonGiao_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllTonGiao(bool clearCache){
		if(_ListTonGiao.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		TonGiaoServiceClient dao = new TonGiaoServiceClient();
		_ListTonGiao=new List<TonGiaoEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public TonGiaoEntity LoadTonGiao(Int32 TonGiao_ID){			
		TonGiaoEntity o=dao.SelectOne(TonGiao_ID);		
		return o;
	}
	#endregion
}
}
